﻿#include <iostream>
#include <Windows.h>
#include "../p002_priority_booster/priority_booster_common.h"

#define SYM_LINK_PRIORITY_BOOSTER L"\\\\.\\PriorityBooster"

int error(const char* msg) {
	printf("%s (%d)\n", msg, GetLastError());
	return 1;
}

int main(int argc, const char* argv[])
{
	if (argc < 3) {
		printf("Usage: p002_r3_booster <threadid> <priority>\n");
		return 0;
	}
	
	HANDLE device_handle = CreateFile(SYM_LINK_PRIORITY_BOOSTER, GENERIC_WRITE,
		FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, nullptr);
	if (device_handle == INVALID_HANDLE_VALUE) {
		return error("打开设备失败");
	}

	ThreadData data;
	data.thread_id_ = atoi(argv[1]);
	data.priority_ = atoi(argv[2]);

	DWORD ret;
	BOOL success = DeviceIoControl(
		device_handle, 
		IOCTL_PRIORITY_BOOSTER_SET_PRIORITY, 
		&data, 
		sizeof(data),
		nullptr,
		0,
		&ret,
		nullptr
		);
	if (success) {
		printf("改变线程优先级成功\n");
	} else {
		printf("改变线程优先级失败\n");
	}
	CloseHandle(device_handle);
}
